package com.demoo.zjsu.service.smartwebsite.zhaoban;

import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.service.smartwebsite.Page;
import com.demoo.zjsu.service.smartwebsite.PageFakeListFactoryExtend;
import com.demoo.zjsu.service.smartwebsite.PageListFactoryExtend;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jshjsh on 2016/9/9.
 * 招生计划等一系列页面
 */
@Service
public class PlanService extends PageListFactoryExtend{


    @Override
    public List<String> getTimes(Elements elements) {
        ArrayList<String> arrayList = new ArrayList<String>();
        for (Element element : elements) {
            String time = element.select(".HSATimeStamp").text();
            arrayList.add(time);
        }
        return arrayList;
    }

    @Override
    public Elements getOtherMain(Document document) {
        return document.select("[width=938]").eq(0);
    }

    @Override
    public PageTwo dealTitle(String url, String title) {
        url = url.replace("html", "").substring(3);
        return new PageTwo(title, "", url, 0);
    }



    @Override
    public PageTwoContent getContent(Elements elements) {
        String content = elements.toString();
        return new PageTwoContent(content,"");
    }

    @Override
    public Page getPage() {
        String[] urls = {"http://zhaoban.zjsu.edu.cn/listbkzn.asp?boardid=51",
                "http://zhaoban.zjsu.edu.cn/listbkzn.asp?boardid=54",
                "http://zhaoban.zjsu.edu.cn/listbkzn.asp?boardid=57",
                "http://zhaoban.zjsu.edu.cn/listzy.asp?boardid=11&page=1",
                "http://zhaoban.zjsu.edu.cn/listzy.asp?boardid=11&page=2",
                "http://zhaoban.zjsu.edu.cn/listzy.asp?boardid=11&page=3"
                ,"http://zhaoban.zjsu.edu.cn/listzy.asp?boardid=12",
                "http://zhaoban.zjsu.edu.cn/listzx.asp",
                "http://zhaoban.zjsu.edu.cn/listzx.asp?page=2"};
        String[] categories = {"招生政策", "招生信息", "就业工作","报考专业","报考专业","报考专业","院系专业","最新消息","最新消息"};
        return new Page(categories, urls);
    }

    @Override
    public Elements getMain(Document document) {
        Elements elements = document.select("#table23");
        int size = elements.select("tr").size();
        elements.select("tr").eq(size-1).remove();
        return elements.select("tr");
    }



    @Override
    public void filterAgain(Elements elements) {
        elements.select("p").eq(0).remove();
        Pattern pattern = Pattern.compile("http://zhaoban.zjsu.edu.cn/html/.*htm");
        Elements a = elements.select("a");
        for (Element element : a) {
            String src = element.attr("href");
            Matcher matcher = pattern.matcher(src);
            if (matcher.matches()) {
                src = src.replace("http://zhaoban.zjsu.edu.cn/html/", "../pagetwocontent/detail4.zjgsu?key=");
                element.attr("href", src);
            }
        }

    }


}
